import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import styleImport from 'vite-plugin-style-import';
import { resolve } from 'path';
import alias from '@rollup/plugin-alias';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';

export default ({ mode }) => {
    // env获取
    const root = process.cwd();
    const env = loadEnv(mode, root);
    return defineConfig({
        optimizeDeps: {
            exclude: ['@x-tech/*'],
        },
        base: env.VITE_ENV === 'local' ? '' : './',
        esbuild: {
            jsxFactory: 'h',
            jsxFragment: 'Fragment',
        },
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@use "@/styles/theme-default.scss" as *;',
                },
            },
        },
        server: {
            port: 8090,
            open: false,
            https: false,
            host: '0.0.0.0',
            proxy: {
                '/pl': {
                    // 要访问的跨域的域名
                    target: env.VITE_URL, // 'http://t-ser-cloud.xinchao.com/portal',
                    changeOrigin: true,
                    autoRewrite: true,
                    secure: false,
                    xfwd: false,
                },
                '/md': {
                    // 要访问的跨域的域名
                    target: env.VITE_URL, // 'http://t-ser-cloud.xinchao.com/portal',
                    changeOrigin: true,
                    autoRewrite: true,
                    secure: false,
                    xfwd: false,
                },
            },
        },
        plugins: [
            vue(),
            vueJsx(),
            styleImport({
                libs: [],
            }),
            createSvgIconsPlugin({
                // Specify the icon folder to be cached
                iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
                // Specify symbolId format
                symbolId: '[name]',
            }),
        ],
        resolve: {
            alias: [{ find: '@', replacement: resolve(__dirname, 'src') }],
        },
        build: {
            rollupOptions: {
                plugins: [alias()],
            },
        },
    });
};
